<?php
namespace app\web\controller;

use app\model\Theme; 
use app\model\Lottery; 

class LtChance extends Base
{
    public function Lt_chance()
    {
/**
 * Created by 底片云
 * Desc:抽奖机会
 * User:cbw
 * Date: 2020/11/29
 */

global $_G,$_P;
$params = $_P;
$themeid = isset($params['themeid'])?$params['themeid']:'';//活动id
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if (!$theme){
    $response = modelShow(40002);
    return_msg($response, '', 'ajax');
}
$config =   Theme::getConfig($themeid);
$system= $_G['system'];
$do = 'Lt_chance';
$ac = !empty($params['ac'])?$params['ac']:'display';
$nickname = isset($params['nickname'])?trim($params['nickname']):'';
$start_day = isset($params['createtime']['start'])?$params['createtime']['start']:time();
$end_day = isset($params['createtime']['end'])?$params['createtime']['end']:time();
$page_index = max(1,intval($params['page']));
$page_size = 10;
$page_start = ($page_index-1)*$page_size;
$lt_item_page_total = intval(db_find("select count(*) as cnt from".tablename(t_lottery))['cnt']);
if ($ac=='display'){
    $page_total = intval(db_find("select count(*) as cnr from ".tablename(t_lot_record))['cnt']);
    $list = Lottery::getDefaultOrSearchChanceList($themeid,$nickname,'','',$page_start,$page_size);
    $pager = pagination($page_total,$page_index,$page_size);
}
if ($ac=='search'){
    if ($start_day==$end_day){
        $nickname = '%'.$nickname.'%';
        $list = Lottery::getDefaultOrSearchChanceList($themeid,$nickname,'','',$page_start,$page_size);
    }else{
        $list = Lottery::getDefaultOrSearchChanceList($themeid,$nickname,$start_day,$end_day,$page_start,$page_size);
    }
}
//扣除抽奖机会
if ($ac=='del'){
    $l_id = $params['ltt_id'];
    $lottery_item = Lottery::getLotteryChanceInfo($l_id);
    if ($lottery_item){
        $insert_data = [
            'uid'=>$lottery_item['uid'],
            'type'=>2,
            'add_key'=>5,
            'op_num'=>(-1*$lottery_item['op_num']),
            'themeid'=>$themeid,
            'today'=>date("Y-m-d",time())
        ];
        $res = Lottery::insertUserRecords($insert_data);
    }
    if ($res){
        $response =  modelShow(200,'',[]);
    }else{
        $response = modelShow(30000,'操作失败',[]);
    }
    return_msg($response, '', 'ajax');
}
$total = count($list);
$return_clicent = get_defined_vars();
return view('/lt_chance', $return_clicent);
}
}